package component
{
	import component.data.ComponentInfo;
	
	import constants.ConfigConst;
	
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	
	import mx.core.UIComponent;
	
	import util.ConfigHelper;
	import util.LocalSwfoader;
	import util.err.FileNotExistErr;
	import util.err.SingletonInstantiationErr;

	public class CmpDataManager
	{
		private static var _instance:CmpDataManager;
		
		private var _cmpInfos:Array;
		
		private var _packageArr:Array;
		
		public function CmpDataManager()
		{
			if( _instance ) throw new SingletonInstantiationErr();
		}
		
		public static function instance():CmpDataManager
		{
			if( !_instance )
			{
				_instance = new CmpDataManager();
			}
			
			return _instance;
		}
		
		/**
		 * 初始化控件库 
		 * 
		 */		
		public function init():void
		{
			//初始化UI组建库
			_cmpInfos = [];
			_packageArr = [];
			
			var configXml:XML = loadConfigXml();
			
			var libPathsXml:XMLList = configXml.lib as XMLList;
			
			for( var i:int = 0; i < libPathsXml.length(); i++ )
			{
				var libNode:XML = libPathsXml[i] as XML;
				
				loadCmpLib( libNode );
			}
		}
		
		
		/**
		 * 读取解析控件库 
		 * @param libXml
		 * 
		 */		
		private function loadCmpLib( libXml:XML ):void
		{
			var cmpInfoXmlList:XMLList = libXml.cmp;
			
			var tmpCmpInfos:Array = [];
			
			var libContextPath:String = String( libXml.@src );
	
			for( var i:int = 0; i < cmpInfoXmlList.length(); i++ )
			{
				var cmpInfoXml:XML = cmpInfoXmlList[i];
				
				var cmpInfo:ComponentInfo = parseCmp( cmpInfoXml );
				
				cmpInfo.libPath = libContextPath;
				
				_cmpInfos.push( cmpInfo );
				
				tmpCmpInfos.push( cmpInfo );
				
			}
			
			loadCmpContext( libContextPath, tmpCmpInfos );
		}
		
		/**
		 * 解析控件库控件信息 
		 * @param xml
		 * @return 
		 * 
		 */		
		private function parseCmp( xml:XML ):ComponentInfo
		{
			var cmpInfo:ComponentInfo = new ComponentInfo();
			
			cmpInfo.className = xml.@name;
			
			return cmpInfo;
			
		}
		
		/**
		 * 加载控件实体对象
		 * @param libContextPath
		 * @param cmpInfos
		 * 
		 */		
		private function loadCmpContext( libContextPath:String, cmpInfos:Array ):void
		{
			var onComplete:Function = function( loader:LoaderInfo ):void{
				
				_packageArr.push( loader );
				
				for( var i:int = 0; i < cmpInfos.length; i++ )
				{
					var cmpInfo:ComponentInfo = cmpInfos[i] as ComponentInfo;
					
					if( loader.applicationDomain.hasDefinition( cmpInfo.className ) )
					{
						var cmpContextClass:Class = loader.applicationDomain.getDefinition( cmpInfo.className ) as Class;
					
						cmpInfo.context = cmpContextClass;
					}
					else
					{
						//相关容错处理
						//initCmp( new Sprite(), cmpInfo );
						trace( "找不到控件实体类!" );
					}
				}
			}
			
			var localSwfLoader:LocalSwfoader = new LocalSwfoader( libContextPath, onComplete );
			
			localSwfLoader.load();
		}
		
		private function loadConfigXml():XML
		{
			return ConfigHelper.loadXmlLocal( ConfigConst.cmpConfigPath );	
		}
		
		
		public function get cmps():Array
		{
			return this._cmpInfos;
		}
	}
}